<template>
  <div>
    <el-form ref="form" :model="skuInfo" label-width="80px">
      <el-form-item label="SPU名称"> {{ spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input
          placeholder="sku名称"
          v-model="skuInfo.skuName"
          style="width: 300px"
        ></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          style="width: 300px"
          v-model.number="skuInfo.price"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(kg)">
        <el-input
          style="width: 300px"
          v-model.number="skuInfo.weight"
        ></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          style="width: 300px"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <!-- 需要收集平台属性id和属性值id，收集在attr.attrIdAndValueId，保证每一个都能收集到，在保存的时候整理数据-->
      <el-form-item label="平台属性">
        <el-form :inline="true">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select v-model="attr.attrIdAndValueId" placeholder="请选择">
              <el-option
                :label="attrValue.valueName"
                :value="`${attr.id}:${attrValue.id}`"
                v-for="attrValue in attr.attrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true">
          <!-- 需要收集销售属性id和属性值id 在保存的时候整理数据-->
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select
              v-model="saleAttr.saleAttrIdAndValueId"
              placeholder="请选择"
            >
              <el-option
                :label="saleAttrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${saleAttrValue.id}`"
                v-for="saleAttrValue in saleAttr.spuSaleAttrValueList"
                :key="saleAttrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item>
        <el-table
          style="width: 100%"
          border
          :data="spuImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            label="label"
            prop="prop"
            width="80"
            type="selection"
            align="center"
          ></el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 82px; height: 82px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设置默认</el-button
              >
              <el-button v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "SkuForm",
  data() {
    return {
      //存储图片信息
      spuImageList: [],
      //存储销售属性
      spuSaleAttrList: [],
      //存储平台属性
      attrInfoList: [],
      spuName: "",
      //收集图片的数据字段
      imageList: [],
      //收集sku数据
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,
        skuName: "",
        price: "",
        weight: "",
        skuDesc: "",
        skuDefaultImg: "",
        skuAttrValueList: [
          // {
          //   valueId: 0,
          //   attrId: 0,
          // },
        ],
        skuImageList: [
          // {
          //    imgName: "string",
          //    imgUrl: "string",
          //    isDefault: "string",
          //    spuImgId: 0,
          // },
        ],
        skuSaleAttrValueList: [
          // {
          //    saleAttrId: 0
          //    saleAttrValueId: 0,
          // },
        ],
      },
    };
  },
  methods: {
    //获取spu数据
    async getData(category1Id, category2Id, spu) {
      //整理数据
      this.skuInfo.category3Id = spu.category3Id;
      this.skuInfo.spuId = spu.id;
      this.skuInfo.tmId = spu.tmId;
      this.spuName = spu.spuName;
      //获取图片
      let result = await this.$API.spu.reqSpuImagelist(spu.id);
      if (result.code == 200) {
        let list = result.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.spuImageList = list;
      }
      // 获取销售属性
      let result1 = await this.$API.spu.reqSpuSaleAttrlist(spu.id);
      if (result1.code == 200) {
        this.spuSaleAttrList = result1.data;
      }
      // 获取平台属性
      let result2 = await this.$API.spu.reqAttrInforlist(
        category1Id,
        category2Id,
        spu.category3Id
      );
      if (result2.code == 200) {
        this.attrInfoList = result2.data;
      }
    },
    //取消展示spu列表数据
    cancel() {
      this.$emit("changeScene", 0);
      Object.assign(this._data, this.$options.data());
    },
    //改变默认图片,排他操作
    changeDefault(row) {
      this.spuImageList.forEach((item) => {
        item.isDefault = 0;
      });
      row.isDefault = 1;
      //收集默认图片地址
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    //table表格复选框事件
    handleSelectionChange(val) {
      this.imageList = val;
    },
    //保存，整理数据提交
    async save() {
      const { skuInfo, attrInfoList, spuSaleAttrList, imageList } = this;
      // 整理平台属性
      attrInfoList.forEach((item) => {
        // 表示平台属性被用户选择
        if (item.attrIdAndValueId) {
          // 分割字符串,attrId,ValueId解析数组
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          // 往对象里面加属性,kv一致省略v
          let skuAttrObj = { attrId, valueId };
          // 添加选中的平台属性id和属性值id
          skuInfo.skuAttrValueList.push(skuAttrObj);
        }
      });
      // 整理销售属性
      spuSaleAttrList.forEach((item) => {
        // 表示销售属性被用户选择
        if (item.saleAttrIdAndValueId) {
          // 分割字符串,attrId,ValueId解析数组
          const [saleAttrId, saleAttrValueId] =
            item.saleAttrIdAndValueId.split(":");
          // 往对象里面加属性,kv一致省略v
          let skuSakeAttrObj = { saleAttrId, saleAttrValueId };
          // 添加选中的平台属性id和属性值id
          skuInfo.skuSaleAttrValueList.push(skuSakeAttrObj);
        }
      });
      //整理图片数据
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });
      let result = await this.$API.spu.reqAddSku(skuInfo);
      if(result.code==200){
        this.$message({
          type:'success',
          message:'添加SKU成功'
        })
      }
      this.$emit('changeScene',0)
    },
  },
};
</script>

<style>
</style>